package org.kiwi.data.util;

import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import static org.kiwi.data.cons.DataConstant.*;

/**
 * Created by jack on 16/12/21.
 */
public class AopUtil {

    public static void registerAopConfig(Element element,
                                         ParserContext parserContext,
                                         String pointCutId,
                                         String pointCutExpression,
                                         String adviceRef,
                                         String adviceOrder) {

        Element aopConfigElement = element.getOwnerDocument().createElementNS(AOP_NAMESPACE_URI, CONFIG);

        //1.declare pointcut
        Element pointcutChild = element.getOwnerDocument().createElementNS(AOP_NAMESPACE_URI, POINTCUT);
        pointcutChild.setAttribute(XSD_ID, pointCutId);
        pointcutChild.setAttribute(EXPRESSION, pointCutExpression);
        aopConfigElement.appendChild(pointcutChild);

        //2.declare advisor
        Element advisorChild = element.getOwnerDocument().createElementNS(AOP_NAMESPACE_URI, ADVISOR);
        advisorChild.setAttribute(POINTCUT_REF, pointCutId);
        advisorChild.setAttribute(ADVICE_REF, adviceRef);
        advisorChild.setAttribute(XSD_ORDER, adviceOrder);
        aopConfigElement.appendChild(advisorChild);

        parserContext.getDelegate().parseCustomElement(aopConfigElement);
    }

}
